package com.edu.service.impl;

import com.edu.bean.User;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Map<String, Object> login(User user) {
        Map<String,Object> map = new HashMap<>();
        User user1 = userMapper.selectUserByName(user.getUserName());
        if (user1 != null) {
            if (user1.getUserPass().equals(user.getUserPass())) {
                map.put("logUser",user1);
            } else {
                map.put("msg","密码错误");
            }
        } else {
            map.put("msg","用户名不存在");
        }
        return map;
    }


    @Override
    @Transactional
    public Map<String,Object> registerUser(User user,int[] hobby) {
        Map<String,Object> map = new HashMap<>();
        if (userMapper.selectUserByName(user.getUserName()) == null) {
            if (userMapper.insertUser(user) > 0) {
                for (int i : hobby) {
                    System.out.println("注册返回ID:"+user.getUserId());
                    System.out.println("hobbyID:"+i);
                    userMapper.insertBookHobby(user.getUserId(),i);
                }
                map.put("registerOk",user.getUserId());
            } else {
                map.put("msg","注册失败");
            }
        } else {
            map.put("msg","用户名重复");
        }
        return map;
    }


}
